/**
 * Copyright (c) 2011 Nokia Corporation.
 */

// Own header
#include "transferuiclientitem.h"

// Harmattan includes
#include <TransferUI/Client>
#include <TransferUI/Transfer>

// Qt includes
#include <QDebug>

// transferuiexample includes
#include "transferuitransferitem.h"


using namespace TransferUI;


/*!
  \class TransferUIClientItem
  \brief QML Wrapper for TransferUI::Client instance.
  \see http://harmattan-dev.nokia.com/docs/library/html/transfer-ui/classTransferUI_1_1Client.html
*/


/*!
  Constructor.
*/
TransferUIClientItem::TransferUIClientItem(QDeclarativeItem *parent)
    : QDeclarativeItem(parent),
      m_client(0),
      m_initialized(false)
{
    setFlag(QGraphicsItem::ItemHasNoContents, true); // Not a visual item

    // Construct the client instance and connect the signals.
    m_client = new Client(this);

    connect(m_client, SIGNAL(cancelTransfer(Transfer*)),
            this, SIGNAL(transferCancelled(Transfer*)));
    connect(m_client, SIGNAL(pauseTransfer(Transfer*)),
            this, SIGNAL(transferPaused(Transfer*)));
    connect(m_client, SIGNAL(repairErrorTransfer(Transfer*)),
            this, SIGNAL(errorRepairForTransferRequested(Transfer*)));
    connect(m_client, SIGNAL(startTransfer(Transfer*)),
            this, SIGNAL(transferStarted(Transfer*)));
    connect(m_client, SIGNAL(summaryReport(int, int, int, int)),
            this, SIGNAL(summaryReport(int, int, int, int)));

    // Initialize the client.
    init();
}


/*!
  Destructor.
*/
TransferUIClientItem::~TransferUIClientItem()
{

}


/*!
  Returns the client instance.
*/
Client *TransferUIClientItem::clientInstance() const
{
    return m_client;
}


/*!
  Returns true if the client has been successfully initialized. False
  otherwise.
*/
bool TransferUIClientItem::initialized() const
{
    return m_initialized;
}


/*!
  \see TransferUI::Client documentation
*/
void TransferUIClientItem::setProgressValueDelta(double delta)
{
    m_client->setProgressValueDelta(delta);
    emit progressValueDeltaChanged(delta);
}


/*!
  \see TransferUI::Client documentation
*/
double TransferUIClientItem::progressValueDelta() const
{
    return m_client->progressValueDelta();
}


/*!
  \see TransferUI::Client documentation
*/
void TransferUIClientItem::setProgressTimeDelta(int delta)
{
    m_client->setProgressTimeDelta(delta);
    emit progressTimeDeltaChanged(delta);
}


/*!
  \see TransferUI::Client documentation
*/
int TransferUIClientItem::progressTimeDelta() const
{
    return m_client->progressTimeDelta();
}


/*!
  \see TransferUI::Client documentation
*/
bool TransferUIClientItem::isTUIVisible() const
{
    return m_client->isTUIVisible();
}


/*!
  Sets the initialization state indicator of the client to \a initialized.
*/
void TransferUIClientItem::setInitialized(bool initialized)
{
    m_initialized = initialized;
    emit initializedChanged(m_initialized);
}


/*!
  Tries to establish the connection between TUIClient and transfer-ui.
  Returns true if successful, false otherwise.
*/
bool TransferUIClientItem::init()
{
    //if (!m_initialized) {
        if (m_client->init()) {
            setInitialized(true);
            return true;
        }
        else {
            qDebug() << "TransferUIClientItem::initialize(): Failed to initialize client!";
        }
    /*}
    else {
        qDebug() << "TransferUIClientItem::initialize(): Client already initialized!";
    }*/

    return false;
}


/*!
  \see TransferUI::Client documentation
*/
bool TransferUIClientItem::askForSummaryUpdate()
{
    return m_client->askForSummaryUpdate();
}


/*!
  \see TransferUI::Client documentation
*/
TransferUITransferItem *TransferUIClientItem::registerTransfer(const QString &name,
                                                               /*TransferType*/ int type,
                                                               const QString &clientId)
{
    qDebug() << "TransferUIClientItem::registerTransfer():" << name << type << clientId;
    QString theClientId = QString();

    if (!clientId.isEmpty()) {
        theClientId = clientId;
    }

    Transfer *transfer = m_client->registerTransfer(name,
                                                    (Client::TransferType)type,
                                                    theClientId);
    TransferUITransferItem *transferItem(0);

    if (transfer) {
        transferItem = new TransferUITransferItem(this);
        transferItem->setClient(this);
        transferItem->setTransfer(transfer);
    }

    return transferItem;
}


/*!
  \see TransferUI::Client documentation
*/
bool TransferUIClientItem::showUI()
{
    return m_client->showUI();
}


/*!
  \see TransferUI::Client documentation
*/
void TransferUIClientItem::removeTransfer(const QString& tid)
{
    m_client->removeTransfer(tid);
}


QML_DECLARE_TYPE(TransferUIClientItem)

// End of file.
